﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Printing;
using DataManager;
using StoreClientModule;
namespace MBB_Store_Sales_Report
{
    public partial class frmCheckStatusViewer : Form
    {

        ChequeRecords cr = new ChequeRecords();
        DataTable RecordTable = new DataTable();
        ChequeEntryCommit cec = new ChequeEntryCommit();

        public frmCheckStatusViewer()
        {
            InitializeComponent();
            btnClose.Click += new EventHandler(btnClose_Click);
            panel2.EnabledChanged += new EventHandler(panel2_EnabledChanged);
            btnPrint.Click +=new EventHandler(btnPrint_Click);
            btnView.Click += new EventHandler(btnView_Click);
            cr.ProcessError += new ChequeRecords.MyEventHandler(cr_ProcessError);
            cr.ProcessCompleted += new ChequeRecords.MyEventHandler(cr_ProcessCompleted);
        }

        void btnView_Click(object sender, EventArgs e)
        {
            if (txtStatus.Text != "")
            {
                cr.SearchStatusRecords(txtStatus.Text);
            }
        }

       

        void cr_ProcessCompleted(string msg)
        {
            uCoreClasses.uThreader t = new uCoreClasses.uThreader();
            t.SafeCall(this, delegate
            {
                if (cr.ProcessCode == 6)
                {
                    RecordTable = cr.RecordTable;
                    grd.DataSource = RecordTable;
                    
                }
            });
        }
        void cr_ProcessError(string msg)
        {
            uCoreClasses.uThreader t = new uCoreClasses.uThreader();
            t.SafeCall(this, delegate
            {
                MessageBox.Show(msg);
            });
        }

        

        void panel2_EnabledChanged(object sender, EventArgs e)
        {
            if (!panel2.Enabled)
            {
                panel2.BackColor = Color.MidnightBlue;
            }
            else
            {
                panel2.BackColor = Color.RoyalBlue;
            }
        }
        void btnClose_Click(object sender, EventArgs e)
        {
            pnlExpensesEntry.Dispose();
            this.Dispose();
        }
        public Panel PanelForm
        {
            get { return pnlExpensesEntry; }
        }
        public void Initialize(string Title)
        {
            this.Select();
            lblText.Text = Title;
        }

        //printing
        int cnt = 0;
        int pp = 1;
        void btnPrint_Click(object sender, EventArgs e)
        {
            PrintDocument pd = new PrintDocument();
            pd.DocumentName = lblText.Text;
            pd.PrintPage += new PrintPageEventHandler(pd_PrintPage);
            PrintDialog pdlg = new PrintDialog();
            pdlg.Document = pd;
            if (pdlg.ShowDialog() == DialogResult.OK)
            {
                PrintPreviewDialog ppdlg = new PrintPreviewDialog();
                ppdlg.Document = pd;
                //Tracking.EnterLog("Price Table is printed.", DateTime.Now.ToString());
                ppdlg.ShowDialog();
            }
        }

        void pd_PrintPage(object sender, PrintPageEventArgs e)
        {
            float lpp = 0;
            float y = 0;
            float tm = e.MarginBounds.Top;
            float lm = e.MarginBounds.Left - 50;
            float rm = e.PageBounds.Width - 150;
            float bm = e.PageBounds.Height - 100;
            float ht = 0;
            Font cFont;
            StringFormat sf = new StringFormat();


            //Header
            y = 110;
            cFont = new Font("Courier New", 11f, FontStyle.Bold);
            ht = cFont.GetHeight(e.Graphics);
            sf.Alignment = StringAlignment.Center;
            e.Graphics.DrawString(Common.lStoreName, new Font("Courier New", 15f, FontStyle.Bold), Brushes.Black, e.PageBounds.Width / 2, 50, sf);
            e.Graphics.DrawString(Common.lStoreAddress, new Font("Courier New", 15f, FontStyle.Bold), Brushes.Black, e.PageBounds.Width / 2, 70, sf);

            sf.Alignment = StringAlignment.Near;
            e.Graphics.DrawString("List of "+txtStatus.Text +" Check - Page " + pp, cFont, Brushes.Black, lm, y, sf);
            sf.Alignment = StringAlignment.Far;
            e.Graphics.DrawString(DateTime.Now.ToShortDateString(), cFont, Brushes.Black, rm + 100, y, sf);
            //Column Header
            y += ht;

            e.Graphics.FillRectangle(Brushes.LightGray, lm, y, rm - lm + 100, ht);
            e.Graphics.DrawRectangle(new Pen(Brushes.Black), lm, y, rm - lm + 100, ht);
            sf.Alignment = StringAlignment.Center;
            float cx = lm + (((rm - lm) * 25) / 100);
            float tx = lm + (((rm - lm) * 12.5f) / 100);
            e.Graphics.DrawLine(new Pen(Brushes.Black), cx, y, cx, y + ht);
            e.Graphics.DrawString("Account Name", cFont, Brushes.Black, tx, y, sf);

            cx += ((rm - lm) * 20) / 100;
            tx = cx - ((rm - lm) * 20) / 200;
            e.Graphics.DrawLine(new Pen(Brushes.Black), cx, y, cx, y + ht);
            e.Graphics.DrawString("Name of Bank", cFont, Brushes.Black, tx, y, sf);

            cx += ((rm - lm) * 18) / 100;
            tx = cx - ((rm - lm) * 18) / 200;
            e.Graphics.DrawLine(new Pen(Brushes.Black), cx, y, cx, y + ht);
            e.Graphics.DrawString("Date Received", new Font("Courier New", 10f, FontStyle.Regular), Brushes.Black, tx, y, sf);
            cx += ((rm - lm) * 18) / 100;
            tx = cx - ((rm - lm) * 18) / 200;
            e.Graphics.DrawLine(new Pen(Brushes.Black), cx, y, cx, y + ht);
            e.Graphics.DrawString("Check No: ", cFont, Brushes.Black, tx, y, sf);
            cx += ((rm - lm) * 18) / 100;
            tx = cx - ((rm - lm) * 18) / 200;
            e.Graphics.DrawLine(new Pen(Brushes.Black), cx, y, cx, y + ht);
            e.Graphics.DrawString("Amount", cFont, Brushes.Black, tx, y, sf);

            tx += ((rm - lm) * 17) / 100;
            e.Graphics.DrawString("Due Date", cFont, Brushes.Black, tx, y, sf);
            //Data
            y += ht * (1.5f);
            cFont = new Font("Courier New", 11f, FontStyle.Regular);
            ht = cFont.GetHeight(e.Graphics);
            lpp = (bm - y) / ht;
            int cl = 0;
            float stx = 0;

            while (cl <= lpp && cnt < grd.Rows.Count)
            {
                sf.Alignment = StringAlignment.Near;
                stx = lm + 1;
                string str = grd.Rows[cnt].Cells["Account Name"].Value.ToString();
                if (str.Length > 16)
                {
                    str = str.Substring(0, 13) + "...";
                }
                e.Graphics.DrawString(str, cFont, Brushes.Black, stx, y, sf);
                stx += ((rm - lm) * 25) / 100;
                str = grd.Rows[cnt].Cells["Name of Bank"].Value.ToString();
                if (str.Length > 13)
                {
                    str = str.Substring(0, 10) + "...";
                }
                e.Graphics.DrawString(str, cFont, Brushes.Black, stx, y, sf);
                stx += ((rm - lm) * 20) / 100;

                str = grd.Rows[cnt].Cells["Date Received"].Value.ToString();
                e.Graphics.DrawString(str, cFont, Brushes.Black, stx, y, sf);
                stx += ((rm - lm) * 18) / 100;

                str = grd.Rows[cnt].Cells["Check Number"].Value.ToString();
                e.Graphics.DrawString(str, cFont, Brushes.Black, stx, y, sf);
                stx += ((rm - lm) * 20) / 100;

                str = grd.Rows[cnt].Cells["Amount"].Value.ToString();
                e.Graphics.DrawString(str, cFont, Brushes.Black, stx, y, sf);

                stx += ((rm - lm) * 18) / 100;

                str = grd.Rows[cnt].Cells["Due Date"].Value.ToString();
                e.Graphics.DrawString(str, cFont, Brushes.Black, stx, y, sf);

                y += ht;
                cnt++;
                cl++;
            }
           
            if (cnt < grd.Rows.Count)
            {
                e.HasMorePages = true;
                pp++;
            }
            else
            {
                e.HasMorePages = false;
                cnt = 1;
                pp = 1;
            }
        }
      
    }
}
